package com.itheima.dubbo.mysql.impl;

import cn.hutool.core.math.MathUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.commons.model.domain.Peach;
import com.itheima.dubbo.interfaces.PeachApi;
import com.itheima.dubbo.mysql.mapper.PeachMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;


@Service
public class PeachApiImpl implements PeachApi {

    @Autowired
    private PeachMapper peachMapper;


    /**
     * 修改语音
     * @param peach
     */
    @Override
    public void insertPeach(Peach peach) {
        peachMapper.insert(peach);
    }

    @Override
    public Peach queryPeach(Long userId) {
        //根据用户id，随机获取一个排除用户id的语音记录
        ArrayList<Long> longs = new ArrayList<>();
        longs.add(userId);
        List<Peach> peaches = peachMapper.selectList(Wrappers
                .lambdaQuery(Peach.class).notIn(Peach::getUserId, longs)
        );

        Peach peach = peaches.get(RandomUtil.randomInt(0,peaches.size()));
        return peach;
    }


}
